﻿using UnityEngine;
using System.Collections;
using PBMessage;
namespace GameUnit
{
    public class AbilityAttack : Ability
    {
        GameScene curScene;

        PBMessage.CS_AbilityEvent attackEvent = new PBMessage.CS_AbilityEvent();

        public override int GetTypeId()
        {
            return (int)AbilityEnum.Attack;
        }

        public override void OnLoad()
        {
            curScene = Game.Scene.Cur<GameScene>();
            if (curScene.LocalPlayer.MainUnit != null && this.Unit.UnitId == curScene.LocalPlayer.MainUnit.UnitId)
            {
                curScene.InputManager.InputEvents += InputManager_AttackEvent;
            }
        }

        /// <summary>
        /// 攻击按键事件
        /// </summary>
        /// <param name="keyCode"></param>
        /// <param name="isDown"></param>
        private void InputManager_AttackEvent(KeyCode keyCode, bool isDown)
        {
            if (this.Unit.IsDie)
            {
                return;
            }
            if (keyCode != KeyCode.Z) return;
            //TODO CD检测
            attackEvent.unitId = this.Unit.UnitId;
            attackEvent.abilityKey = this.AbilityKey;
            Game.Net.SendPacket<PBMessage.CS_AbilityEvent>(attackEvent, PBMessageID.CS_AbilityEvent);
        }

        public override void HandlerEvent(SC_AbilityEvent abilityEvent)
        {
            foreach (Weapon weapon in this.Unit.Weapons)
            {
                weapon.ExecAttack();
            }
        }
    }
}